/**
 * 自定义查询组件
 * 依赖meeting.core.js
 */
miiteen.customsearch = {
    open: function (code, selectFn, option) {
        var defaultOption = {
            title: "",
            type: "list",
            multiple: false,
            rootId: 1
        }

        if (option == null) {
            option = defaultOption;
        } else {
            option.con = option.con ? option.con : {};
        }

        var selectDialog;
        switch (option.type) {
            case "list":
                if (option.multiple) {
                    selectDialog = window.parent.multipleGrid;
                } else {
                    selectDialog = window.parent.singleGrid;
                }
                break;
            case "tree":
                if (option.multiple) {
                    selectDialog = window.parent.multipleTree;
                } else {
                    selectDialog = window.parent.singleTree;
                }
                break;
        }

        //获取选择结果事件
        selectDialog.getSelect = function () {
            selectFn(selectDialog.selectitems);
            selectDialog.visible = false;
        }
        //初始化界面
        miiteen.customsearch.getcolsetting(code, function (response) {
            var result = response.data;
            if (result.state > 0) {
                selectDialog.sqlmapId = result.data.sqlMapId;
                selectDialog.cols = result.data.colList;
                selectDialog.controls = result.data.controlList;
                var selectDialogCon = {};
                if (selectDialog.controls && selectDialog.controls.length > 0) {
                    for (var ci = 0; ci < selectDialog.controls.length; ci++) {
                        var control = selectDialog.controls[ci];
                        selectDialogCon[control.fieldName] = control.qcDefault;
                    }
                }
                selectDialog.con = selectDialogCon;
                selectDialog.visible = true;
                selectDialog.search = function () {
                    var wheresql = "";
                    for (var c in selectDialog.con) {
                        if (selectDialog.con[c] && selectDialog.con[c].trim() != "") {
                            wheresql += " and " + c + " like '%" + selectDialog.con[c].trim() + "%'";
                        }
                    }
                    option.con["whereSql"] = wheresql;
                    miiteen.customsearch.search(result.data.sqlMapId, option.con, function (resp) {
                        var result = resp.data;

                        switch (option.type) {
                            case "list":
                                selectDialog.gridData = result.data;
                                break;
                            case "tree":
                                selectDialog.gridData = AlgorithmsUtils.simpleToTree(result.data, "result1", "result11");
                                break;
                        }
                    })
                }
                miiteen.customsearch.search(result.data.sqlMapId, option.con, function (resp) {
                    var result = resp.data;

                    switch (option.type) {
                        case "list":
                            selectDialog.gridData = result.data;
                            break;
                        case "tree":
                            selectDialog.gridData = AlgorithmsUtils.simpleToTree(result.data, "result1", "result11");
                            break;
                    }
                })
            } else {
                alert("打开弹窗")
            }
        })
    },
    //获取弹窗内容
    getcolsetting: function (code, fn) {
        miiteen.ajax.get("/api/module/customsearch/get_col_setting?code=" + code, fn);
    },
    //查询弹窗数据
    search: function (sqlId, con, fn) {
        miiteen.ajax.post("/api/module/customsearch/search", {
            sqlId: sqlId,
            con: con
        }, fn);
    }
}